---
id: db
title: 2. Db 静态类
sidebar_label: 2. Db 静态类
---

## 2.1 获取非泛型仓储

```cs
var repository = Db.GetRepository();
```

## 2.2 获取泛型仓储

```cs
var entityRepository = Db.GetRepository<TEntity>();
```

## 2.3 获取带定位器泛型仓储

```cs
var locatorRepository = Db.GetRepository<TEntity, TDbContextLocator>();
```

## 2.4 获取 `Sql` 仓储

```cs
var sqlRepository = Db.GetSqlRepository();
```

## 2.5 获取 `Sql` 定位器仓储

```cs
var sqlLocatorRepository = Db.GetSqlRepository<TDbContextLocator>();
```

## 2.6 获取 `Sql` 代理对象

```cs
var sqlProxy= Db.GetSqlProxy<TSqlDispatchProxy>();
```

## 2.7 获取默认数据库上下文

```cs
var dbContext = Db.GetDbContext();
```

## 2.8 获取定位器数据库上下文

```cs
var locatorDbContext = Db.GetDbContext<TDbContextLocator>();
var locatorDbContext2 = Db.GetDbContext(typeof(TDbContextLocator));
```

## 2.9 创建新的默认数据库上下文

```cs
var dbContext = Db.GetNewDbContext();
```

## 2.10 创建新的定位器数据库上下文

```cs
var locatorDbContext = Db.GetNewDbContext<TDbContextLocator>();
var locatorDbContext2 = Db.GetNewDbContext(typeof(TDbContextLocator));
```

## 2.11 根据定位器类型获取仓储

```cs
var repository = Db.GetRepository<TEntity>(dbContextLocatorType);
```

## 2.12 获取 `主从库` 仓储

```cs
var msRepository = Db.GetMSRepository();
```

## 2.13 获取 `Sql` 主库定位器仓储

```cs
var msLocatorRepository = Db.GetMSRepository<TMasterDbContextLocator>();
```

## 2.14 获取特定定位器仓储

```cs
var msLocatorRepository = Db.GetDbRepository<TDbContextLocator>();
```